//Bonus
var Bonus = function () {
	this.bounds = {
		x : 0,
		y : 0,
		w : 16,
		h : 16
	};
	
	// Contiendra le type du bonus que l'on utilisera
	this.bonusType = null;
	this.type = '';
	
	this.disappear = false;
	
	this.freeze = {
		color	: '#0ff',
		text	: 'F',
		time 	: 3
	};

	this.money = {
		color 	: '#E54910',
		text 	: '$',
		time	: 6
	};

	this.gravity = {
		color 	: '#543F1E',
		text 	: 'G',
		time	: 8,
		particules : []
	};

	this.life = {
		color 	: '#BF0B71',
		text 	: '+',
		time 	: 1
	}

	this.nocolor = {
		color 	: '#FFF',
		text 	: '*',
		time 	: 4
	}
	
	// Initialisation
	this.init = function(typeOfBonus) {		 
		this.bounds.x = rand(this.bounds.w,MAX_WIDTH-this.bounds.w);
		this.bounds.y = rand(this.bounds.h,MAX_HEIGHT-this.bounds.h);
		this.type = typeOfBonus;
		switch (this.type) {
			case 'freeze' : 	this.bonusType = this.freeze;
								break;

			case 'money': 		this.bonusType = this.money;
								break;

			case 'gravity': 	this.bonusType = this.gravity;
								break;

			case 'life': 		this.bonusType = this.life;
								break;

			case 'nocolor': 	this.bonusType = this.nocolor;
								break;
		}
	};
	this.testit = false;
	// Rendu visuel du bonus
	this.render = function(context) {
		if (game.bonus != null && !game.bonus.disappear){
			context.fillStyle = this.bonusType.color;
			context.shadowColor = this.bonusType.color;
			context.shadowBlur = 10;
			context.shadowOffsetX = 0;
			context.shadowOffsetY = 0;
			context.fillRect(this.bounds.x, this.bounds.y, this.bounds.w, this.bounds.h);
			context.fillStyle = '#BBB';
			if (this.type == 'nocolor') context.fillStyle = '#333';
			context.font = "bold " + 12 + "pt MolotRegular";
			context.fillText( this.bonusType.text, this.bounds.x + 3, this.bounds.y + 13 );
		}

		if(game.bonus.type == 'gravity'){
			if(this.gravity.particules.length > 0){
				animParticles(this.gravity.particules, 'gravity');
				renderParticules(this.gravity.particules, '#FFF');
			}
		}
	};
	
	// Gestion de la collision
	// Return False s'il y a collision
	this.collide = function(xPos,yPos) {
		var xTopLeft = this.bounds.x;
		var yTopLeft = this.bounds.y;
		var xBottomRight = this.bounds.x + this.bounds.w;
		var yBottomRight = this.bounds.y + this.bounds.h;

		if (xPos > xTopLeft && xPos < xBottomRight && yPos > yTopLeft && yPos < yBottomRight) return true;
		else return false;
	};

	// Disparition du bonus
	this.hide = function() {
		this.bounds.x = -30;
		this.bounds.y = -30;
	}
	
	// Active le bonus qui a été généré
	this.activate = function() { 
		switch (this.type) {
			case 'freeze' : 	this.activateFreeze();
								break;

			case 'money': 		this.activateMoney();
								break;
			
			case 'gravity': 	this.activateGravity();
								break;

			case 'life': 		this.activateLife();
								break;
			
			case 'nocolor': 	this.activateNocolor();
								break;
		}
		this.hide();
	};
	// Désactive le bonus qui a été généré
	this.desactivate = function() {
		switch (this.type) {
			case 'freeze' : 	this.desactivateFreeze();
								break;

			case 'money': 		this.desactivateMoney();
								break;

			case 'gravity': 	this.desactivateGravity();
								break;

			case 'life': 		this.desactivateLife();
								break;

			case 'nocolor': 	this.desactivateNocolor();
								break;
		}
	};
	
	// Freeze le jeu pendant un moment
	this.activateFreeze = function() {
		game.p1.flash('blue');
		GAME_SPEED = 0;
	};
	// Unfreeze le jeu
	this.desactivateFreeze = function() {
		GAME_SPEED = 1;
	};

	// Diminue le temps entre chaque revenue
	this.activateMoney = function() {
		game.p1.flash('orange');
		game.cashPeriod = 1;
	};
	// Restaure la valeur normale pour le cash
	this.desactivateMoney = function() {
		game.cashPeriod = 10;
	};

	// Attire les followers vers l'endroit du piège
	this.activateGravity = function() {
		game.xFollower = this.bounds.x;
		game.yFollower = this.bounds.y;
		game.offSetFollowers = 10;
	};
	// Libère les followers
	this.desactivateGravity = function() {
		game.xFollower = game.p1.bounds.x;
		game.yFollower = game.p1.bounds.y;
		game.offSetFollowers = 1;
	};

	// Redonne un tick de vie au joueur
	this.activateLife = function() {
		game.p1.resize(6);
	};
	// ??
	this.desactivateLife = function() {
	};

	// Rend tous les ennemis comestible
	this.activateNocolor = function() {
		game.p1.flash('white');
	};
	// Retour à un mode normal
	this.desactivateNocolor = function() {
	};

	//Explose la particule
	this.showGravity = function(n){
		this.gravity.particules = [];
		var N = ((2 * Math.PI) / n);
		var speed = 3;
		while(n --){
			//Variable selon le n
			var angle = N * n;
			this.gravity.particules.push(
				{
					x : game.xFollower + Math.sin(angle) * 80,
					y : game.yFollower + Math.cos(angle) * 80,
					xs: -Math.sin(angle) * speed/2,
					ys: -Math.cos(angle) * speed/2,
					l : this.bonusType.time * 35 / 5,
					s : 2,
					d : 5,
					c : 'rgba(255, 255, 255, '+Math.random()+')'
				}
			);
		}
	}
};

